package com.hualude.roadsign.repository;

import com.hualude.roadsign.dto.DTMainTenanceDto;
import com.hualude.roadsign.dto.DTRequestDto;
import com.hualude.roadsign.dto.MaintenanceDto;
import com.hualude.roadsign.model.MaintenanceEntity;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by ding on 2017/3/3.
 */
public interface MaintenanceRepository extends CrudRepository<MaintenanceEntity, Long> {
    /**
     * @Description: 获取用于DT显示维修单信息
     * @Author: jiang-weirong
     * @Date: 2017/3/3 16:46
     */
    public List<DTMainTenanceDto> getMaintenance4DT(String text, Integer isRead, DTRequestDto dtRequestDto, Integer roadSignId) throws Exception;

    /**
     * @Description: 获取用于DT显示维修单信息
     * @Author: jiang-weirong
     * @Date: 2017/3/3 16:46
     */
    public Integer getMaintenanceCount4DT(String text, Integer isRead, Integer roadSignId) throws Exception;


    public String getmainPersonNameById(Integer mainPerson) throws Exception;

    public String getcompanyPersonNameById(Integer companyPerson) throws Exception;

    public String getmaintenancePeopleNameById(Integer maintenancePeopleId) throws Exception;

    public Integer updateMaintenanceById(Integer maintenanceId, String maintenanceTime, String maintenanceContent, String maintenancePic, Integer maintenanceSignStatus, String maintenanceFeedback) throws Exception;

    public Integer saveFeedbackInfo(Integer maintenanceId, String maintenanceFeedback) throws Exception;

    @Modifying
    @Transactional
    @Query(value = "update MaintenanceEntity m set  m.maintenanceStatus=?1 where m.maintenanceId =?2")
    public Integer upMaintenanceStatu(Integer maintenanceStatu, Integer maintenanceId) throws Exception;

    @Query(value = "SELECT COUNT(*) FROM MaintenanceEntity where companyCheck=1 AND mainCheck = 1 AND  concat(companyPersonName,mainPersonName,maintenancePeopleName) like %?1%")
    Integer getMaintenanceCount4DTo(String text) throws Exception;

    /**
     * @Description: 根据维修Id获取已维修信息
     * @Author: shao-yikun
     * @Date: 2017/3/15 20:53
     */
    @Query(value = "select new com.hualude.roadsign.dto.MaintenanceDto(mt.maintenanceId, mt.roadSignId, mt.maintenanceReason, " +
            "mt.maintenanceLevel, mt.maintenanceType, mt.maintenancePic, mt.maintenanceContent, mt.maintenanceFeedback, " +
            "mt.maintenanceExplain, mt.maintenanceSignStatus, mt.maintenanceTime, mt.maintenanceStatus, mt.maintenancePeopleId, " +
            "mt.maintenanceCreateTime, mt.signFaultPic, rp.roadPoleId, rp.longitude, rp.latitude, rp.province, rp.city, rp.area, " +
            "rp.roadName, rp.startRoad, rp.endRoad, rs.roadSignPic) " +
            "from MaintenanceEntity mt, RoadsignEntity rs, RoadpoleEntity rp " +
            "where mt.roadSignId = rs.roadSignId and rp.roadPoleId = rs.roadPoleId and mt.maintenanceId = ?1")
    public MaintenanceDto getMaintenanceByMaintenanceId(Integer maintenanceId) throws Exception;

    /**
     * @Description: 根据维修Id获取已维修信息
     * @Author: shao-yikun
     * @Date: 2017/3/15 20:53
     */
    @Query(value = "select new com.hualude.roadsign.dto.MaintenanceDto(mt.maintenanceId, mt.roadSignId, mt.maintenanceReason, " +
            "mt.maintenanceLevel, mt.maintenanceType, mt.maintenancePic, mt.maintenanceContent, mt.maintenanceFeedback, " +
            "mt.maintenanceExplain, mt.maintenanceSignStatus, mt.maintenanceTime, mt.maintenanceStatus, mt.maintenancePeopleId, " +
            "mt.maintenanceCreateTime, mt.signFaultPic, rp.roadPoleId, rp.longitude, rp.latitude, rp.province, rp.city, rp.area, " +
            "rp.roadName, rp.startRoad, rp.endRoad, rs.roadSignPic, ur.userName) " +
            "from MaintenanceEntity mt, RoadsignEntity rs, RoadpoleEntity rp, UserEntity ur " +
            "where mt.roadSignId = rs.roadSignId and rp.roadPoleId = rs.roadPoleId " +
            "and ur.userId = mt.maintenancePeopleId and mt.maintenanceId = ?1")
    public MaintenanceDto getReceiptByMaintenanceId(Integer maintenanceId) throws Exception;

    /**
     * @Description: 通过signFaultId获取对应的故障信息
     * @Author: xu-jiaHui
     * @Date: 2017/3/22 10:47
     */
    @Query(value = "SELECT new com.hualude.roadsign.dto.MaintenanceDto(maintenanceContent,maintenanceFeedback,maintenanceTime,maintenanceStatus,maintenanceCreateTime) FROM MaintenanceEntity  where signFaultId=?1")
    public List<MaintenanceDto> getMaintenanceById(Integer signFaultId) throws Exception;


    /**
     * @Description: 为故障处理页面获取维护信息
     * @Author: xu-jiaHui
     * @Date: 2017/3/31 0:53
     */
    public List<MaintenanceDto> getMaintenance4Disposal(DTRequestDto dtRequestDto, Integer actorId, Integer userId) throws Exception;

    public Integer getMaintenanceCount4Disposal(DTRequestDto dtRequestDto, Integer actorId, Integer userId) throws Exception;

    /**
     * @Description: 故障处理企业审核
     * @Author: xu-jiaHui
     * @Date: 2017/3/31 15:41
     */
    @Modifying
    @Query(value = "update MaintenanceEntity set companyCheck=?2 where maintenanceId=?1")
    public Integer updateCompanyCheck(Integer maintenanceId, Integer isPass) throws Exception;

    /**
     * @Description: 报修审批主管审核
     * @Author: xu-jiaHui
     * @Date: 2017/4/4 21:20
     */
    @Modifying
    @Query(value = "update MaintenanceEntity set mainCheck=?2 where maintenanceId=?1")
    public Integer updateMainCheck(Integer maintenanceId, Integer isPass) throws Exception;

    /**
     * @Description: 为当前工单页面获取数据4DT
     * @Author: xu-jiaHui
     * @Date: 2017/4/1 20:08
     */
    public List<MaintenanceDto> getMaintenance4Order(DTRequestDto dtRequestDto, Integer actorId, Integer userId) throws Exception;

    public Integer getMaintenanceCount4Order(DTRequestDto dtRequestDto, Integer actorId, Integer userId) throws Exception;

    /**
     * @Description: 根据userId获取故障信息用于追踪进度页面
     * @Author: xu-jiaHui
     * @Date: 2017/4/1 23:09
     */
    public List<MaintenanceDto> getMaintenanceByUserId(DTRequestDto dtRequestDto, Integer actorId, Integer userId) throws Exception;

    public Integer getMaintenanceCountByUserId(DTRequestDto dtRequestDto, Integer actorId, Integer userId) throws Exception;

    /**
     * @Description: 根据标牌Id删除维修记录
     * @Author: shao-yikun
     * @Date: 2017/4/10 19:19
     */
    @Modifying
    @Query(value = "delete from MaintenanceEntity where roadSignId = ?1")
    public Integer deleteMaintenanceByRoadSignId(Integer roadSignId) throws Exception;

    /**
     * @Description: 获取故障信息
     * @Author: shao-yikun
     * @Date: 2017/4/11 23:57
     */
    public List<MaintenanceDto> getFaultHandle4DT(DTRequestDto dtRequestDto,
                                                  Integer orgId,
                                                  Integer maintenanceStatus,
                                                  String actorIds) throws Exception;

    /**
     * @Description: 获取故障信息数量
     * @Author: shao-yikun
     * @Date: 2017/4/12 0:01
     */
    public Integer getCount4DT(String search,
                               Integer orgId,
                               Integer maintenanceStatus,
                               String actorIds) throws Exception;

    /**
     * @Description: 获取故障信息
     * @Author: shao-yikun
     * @Date: 2017/4/11 23:57
     */
    public List<MaintenanceDto> getFaultHandle(Integer orgId,
                                               Integer maintenanceStatus,
                                               String actorIds,
                                               Integer mCount,
                                               Integer mySize) throws Exception;


    /**
     * @Description: 添加施工方案
     * @Author: shao-yikun
     * @Date: 2017/4/12 14:24
     */
    @Modifying
    @Query(value = "update MaintenanceEntity set maintenanceContent = ?2, maintenanceStatus = 1 " +
            "where maintenanceId = ?1")
    public Integer addMaintenanceContent(Integer maintenanceId, String maintenanceContent) throws Exception;

    /**
     * @Description: 修改故障维修表
     * @Author: shao-yikun
     * @Date: 2017/4/12 21:47
     */
    @Modifying
    @Query(value = "update MaintenanceEntity set maintenanceSignStatus = ?2, maintenanceExplain = ?3, " +
            "maintenancePic = ?4, maintenancePeopleId = ?5, maintenanceTime = ?6, " +
            "maintenanceStatus = 2 where maintenanceId = ?1")
    public Integer updateMaintenance(Integer maintenanceId,
                                     Integer maintenanceSignStatus,
                                     String maintenanceExplain,
                                     String maintenancePic,
                                     Integer maintenancePeopleId,
                                     String maintenanceTime) throws Exception;

    /**
     * @Description: 根据roadSignId 获取 maintenance表中最新的维修信息
     * @Author: xu-jiaHui
     * @Date: 2017/5/16 21:37
     */
    public Integer getMaintenanceStatusByRoadSignId(Integer roadSignId) throws Exception;

    /**
     * @Description: 获取标牌维修状态
     * @Author: jiang-weirong
     * @Date: 2017/5/18 21:53
     */
    @Modifying
    @Query(value = "SELECT m.maintenanceStatus\n" +
            "FROM MaintenanceEntity m\n" +
            "WHERE m.signFaultId=?1 ORDER BY m.maintenanceCreateTime DESC")
    public List<Integer> getMaintenanceStatus(Integer signFaultId) throws Exception;


}
